//
//  RETSkuItemView.m
//  Retail
//
//  Created by ChenJie on 4/2/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "RETSkuItemView.h"
#import "OASDK.h"

#define kTitleColorNormal OAColor(333333)
#define kBorderColorNormal OAColor(d5d5d5)
#define kTitleColorSelected OAColor(E43494)
#define kBorderColorSelected OAColor(E43494)


@interface RETSkuItemView ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *selectedSignView;
@property (nonatomic, assign) BOOL selected;

@end

@implementation RETSkuItemView

#pragma mark - Lifecycle methods

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:self.titleLabel];
        [self addSubview:self.selectedSignView];
        
        self.layer.borderWidth = 1;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemDidClick:)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];
}

- (void)didMoveToSuperview {
    [super didMoveToSuperview];
}

- (void)willMoveToWindow:(UIWindow *)newWindow {
    [super willMoveToWindow:newWindow];
}

- (void)didMoveToWindow {
    [super didMoveToWindow];
}


#define kSelectedItemSignDimension 20

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //
    self.titleLabel.frame = self.bounds;
    
    CGFloat selectedSignLeft = self.oa_width - kSelectedItemSignDimension;
    CGFloat selectedSignTop = self.oa_height - kSelectedItemSignDimension;
    self.selectedSignView.frame = CGRectMake(selectedSignLeft, selectedSignTop, kSelectedItemSignDimension, kSelectedItemSignDimension);
}

#pragma mark - Public methods



#pragma mark - Private methods



#pragma mark - Event response methods

- (void)itemDidClick:(UITapGestureRecognizer *)tap {
    !self.itemDidClick ?: self.itemDidClick(self.viewModel);
}

#pragma mark - Delegate methods



#pragma mark - Setter

- (void)setViewModel:(RETSkuItemViewModel *)viewModel {
    _viewModel = viewModel;
    
    self.titleLabel.text = viewModel.title;
    self.selected = viewModel.selected;
}

- (void)setSelected:(BOOL)selected {
    _selected = selected;
    
    if (selected) {
        self.titleLabel.textColor = kTitleColorSelected;
        self.layer.borderColor = kBorderColorSelected.CGColor;
        self.selectedSignView.hidden = NO;
    } else {
        self.titleLabel.textColor = kTitleColorNormal;
        self.layer.borderColor = kBorderColorNormal.CGColor;
        self.selectedSignView.hidden = YES;
    }
}

#pragma mark - Getter

OAGetLabel(titleLabel, ^(UILabel *label){
    label.font = [UIFont systemFontOfSize:16];
    label.textColor = kTitleColorNormal;
    label.textAlignment = NSTextAlignmentCenter;
})

OAGetImageView(selectedSignView, ^(UIImageView *imageView){
    imageView.image = [UIImage imageNamed:@"selectedItemSign"];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.hidden = YES;
})

#pragma mark - Memory management methods

- (void)dealloc {
}

@end
